/***************************************************************************
 * This program is free software; you can redistribute it and/or           *
 * modify it under the terms of the GNU General Public License             *
 * as published by the Free Software Foundation; either version 2          *
 * of the License, or (at your option) any later version.                  *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program; if not, write to the Free Software Foundation, *
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.         *
 *                                                                         *
 *   Copyright (C) 2007,2008 by Ivan Vucica                                *
 *   ivucica@gmail.com                                                     *
 ***************************************************************************/

#include <string>
#include <sstream>

std::string orderednumber (int num) {
	std::stringstream s;
	if (num < 10) {
		switch(num) {
			case 0:
				s << "number zero";
				break;
			case 1:
				s << "first";
				break;
			case 2:
				s << "second";
				break;
			case 3:
				s << "third";
				break;
			case 4:
				s << "fourth";
				break;
			case 5:
				s << "fifth";
				break;
			case 6:
				s << "sixth";
				break;
			case 7:
				s << "seventh";
				break;
			case 8:
				s << "eigth";
				break;
			case 9:
				s << "ninth";
				break;
		}
		return s.str();
	}
	if (num>=10 && num < 20) {
		s << num << "th";
		return s.str();
	}
	switch (num % 10) {
		case 1:
			s << num << "st";
			break;
		case 2:
			s << num << "nd";
			break;
		case 3:
			s << num << "rd";
			break;
		default:
			s << num << "th";
			break;
	}
	return s.str();
}


void str_replace(std::string &s, const std::string& what, const std::string& with) {
    std::string::size_type p, l;
    for (l = 0; (p = s.find(what, l)) != std::string::npos; l = p + with.size()) {
        s.replace(p, what.size(), with);
    }
}

std::string str_replace(const std::string &s, const std::string& what, const std::string& with) {
	std::string r = s;
	str_replace(r, what, with);
	return r;
}
